package com.mpyy.service.sales;

import com.mpyy.dao.sales.ProSalesMapperDaoInter;
import com.mpyy.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description ProSalesServiceImpl
 * @Author qzy
 * @Date 2022-03-07 8:19
 * @Version 1.0
 */
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Service(value = "proSalesServiceImpl")
public class ProSalesServiceImpl implements ProSalesServiceInter {
    @Autowired
    @Qualifier(value = "proSalesMapperDaoInter")
    private ProSalesMapperDaoInter proSalesMapperDaoInter;

    /**
     * 查询库存明细表，获取商品信息，填充到商品清单的tab栏
     * @param inveDescDepot 所在库存名称
     * @param paging 分页对象
     * @return List<InventoryDesc> 库存明细对象集合
     */
    @Override
    public List<InventoryDesc> searchProBill(String inveDescDepot, Paging paging) {
        return proSalesMapperDaoInter.searchProBill(inveDescDepot,paging.getCurrentPage(),paging.getLimit());
    }

    /**
     * 查询库存明细表中的数据条数
     * @return int 返回数据总数
     */
    @Override
    public int searchProBillsCount() {
        return proSalesMapperDaoInter.searchProBillsCount();
    }

    /**
     * 根据商品编号查询该商品批次信息
     * @param inventoryDesc 库存明细对象
     * @return List<InventoryDesc> 库存明细对象集合
     */
    @Override
    public List<InventoryDesc> searchProBatchNumInfo(InventoryDesc inventoryDesc) {
        return proSalesMapperDaoInter.searchProBatchNumInfo(inventoryDesc);
    }

    /**
     * 新增商品销售信息
     * @param saleOrder 销售订单对象
     * @param saleOrderDescs 销售订单明细对象
     * @return boolean 是否新增成功
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    @Override
    public boolean insertProSalesInfo(SaleOrder saleOrder, List<SaleOrderDesc> saleOrderDescs) {
        //新增销售订单信息
        int row1 = proSalesMapperDaoInter.updateInsertSalesOrdInfo(saleOrder);

        //新增销售订单明细信息
        int row2 = proSalesMapperDaoInter.updateInsertSalesOrdDescInfo(saleOrderDescs);
        
        int row3 = 0;
        for(SaleOrderDesc saleOrderDesc : saleOrderDescs){
            //实例化一个库存明细对象
            InventoryDesc inventoryDesc = new InventoryDesc();
            inventoryDesc.setProId(saleOrderDesc.getProductId());
            inventoryDesc.setInveDescBatchNum(saleOrderDesc.getSalOrdDescProBatchNum());
            inventoryDesc.setInveDescInventoryNum(saleOrderDesc.getSalOrdDescProNum());
            //更改库存信息
            int row = proSalesMapperDaoInter.updateAlertInventoryDescInfo(inventoryDesc);
            row3 += row;
        }
        
        //新增库存变动信息
        int row4 = proSalesMapperDaoInter.updateInsertInventoryChangeInfo(saleOrderDescs);

        //判断是否新增成功
        boolean isSuccess = false;
        if(row1==1 && row2==saleOrderDescs.size() && row3==saleOrderDescs.size() && row4==saleOrderDescs.size()){
            isSuccess = true;
        }
        return isSuccess;
    }

    /**
     * 查询客户信息
     * @return List<CustomerInfo> 客户对象集合
     */
    @Override
    public List<CustomerInfo> searchCustomerInfo() {
        return proSalesMapperDaoInter.searchCustomerInfo();
    }

    /**
     * 查询仓库信息
     * @return List<DepotInfo> 仓库对象集合
     */
    @Override
    public List<DepotInfo> searchDepotInfo() {
        return proSalesMapperDaoInter.searchDepotInfo();
    }

    /**
     * 查询员工信息(经办人)
     * @return List<EmployeeInfo> 员工对象集合
     */
    @Override
    public List<EmployeeInfo> searchEmpInfo() {
        return proSalesMapperDaoInter.searchEmpInfo();
    }

    /**
     * 查询客户类型信息(经办人)
     * @return List<CustomerType> 客户类型对象集合
     */
    @Override
    public List<CustomerType> searchCustomerTypeInfo() {
        return proSalesMapperDaoInter.searchCustomerTypeInfo();
    }

    /**
     * 修改客户信息
     * @param customerInfo 客户信息对象
     * @return 影响行数
     */
    @Override
    public int updateAlertCustomerInfo(CustomerInfo customerInfo) {
        return proSalesMapperDaoInter.updateAlertCustomerInfo(customerInfo);
    }

    /**
     * 新增客户信息
     * @param customerInfo 客户信息对象
     * @return 影响行数
     */
    @Override
    public int updateInsertCustomerInfo(CustomerInfo customerInfo) {
        return proSalesMapperDaoInter.updateInsertCustomerInfo(customerInfo);
    }

    /**
     * 带条件查找商品清单信息
     * @param inventoryDesc 库存明细对象
     * @return 库存明细对象集合
     */
    @Override
    public List<InventoryDesc> searchProBillInfo(InventoryDesc inventoryDesc) {
        return proSalesMapperDaoInter.searchProBillInfo(inventoryDesc);
    }

    /**
     * 根据商品进货日期查询最近进货的商品信息（近一个月内）
     * @return List<InventoryDesc> 库存明细对象集合
     */
    @Override
    public List<InventoryDesc> searchRecentPurchaseInfo() {
        return proSalesMapperDaoInter.searchRecentPurchaseInfo();
    }

    /**
     * 查询商品级别表所有数据
     * @return List<ProductType> 商品级别对象集合
     */
    @Override
    public List<ProductType> searchProductTypeInfo() {
        return proSalesMapperDaoInter.searchProductTypeInfo(null);
    }

    /**
     * 查询商品列表的信息【所在仓库 | 商品类别编号（商品编号）】
     * @param proTypeId 商品级别编号
     * @return 库存明细对象集合
     */
    @Override
    public List<InventoryDesc> searchProListTree(String proTypeId) {
        return proSalesMapperDaoInter.searchProListTree(proTypeId);
    }

    /**
     * 查询商品销售历史信息
     * @param saleOrderDesc 商品明细对象
     * @return List<SaleOrderDesc> 销售订单明细对象集合
     */
    @Override
    public List<SaleOrderDesc> searchProSaleHisInfo(SaleOrderDesc saleOrderDesc) {
        return proSalesMapperDaoInter.searchProSaleHisInfo(saleOrderDesc);
    }
}
